Java.lang প্যাকেজের মধ্যে থাকা String
, StringBuilder
, এবং StringBuffer
হল তিনটি গুরুত্বপূর্ণ ক্লাস যা স্ট্রিং সম্পর্কিত কাজের জন্য ব্যবহৃত হয়। তবে, এই তিনটির মধ্যে পারফরম্যান্স এবং কার্যকারিতা নিয়ে কিছু পার্থক্য রয়েছে, যা আপনাকে পরিস্থিতি অনুযায়ী সঠিক ক্লাস নির্বাচন করতে সহায়তা করবে।
নিচে String, StringBuilder, এবং StringBuffer এর পারফরম্যান্স পার্থক্য এবং ব্যবহারিক দিকগুলি বিস্তারিতভাবে আলোচনা করা হলো।
String হল Java-র সবচেয়ে মৌলিক ডেটা টাইপ এবং এটি immutable (অপরিবর্তনীয়)। এর মানে হল যে একবার একটি String
অবজেক্ট তৈরি হলে, আপনি এর মান পরিবর্তন করতে পারবেন না। যদি আপনি একটি নতুন মান সেট করেন, তবে এটি একটি নতুন String
অবজেক্ট তৈরি করবে এবং পুরোনো অবজেক্টটি অব্যবহৃত অবস্থায় থাকবে। এটি মেমরি ব্যবহারে কিছুটা অকার্যকর হতে পারে, যদি অনেকগুলি স্ট্রিং অপারেশন একের পর এক করা হয়।
String str = "Hello";
str = str + " World"; // A new String object is created, old "Hello" is discarded
StringBuilder একটি mutable (পরিবর্তনযোগ্য) ক্লাস, যা স্ট্রিং ম্যানিপুলেশনকে অনেক দ্রুত এবং দক্ষ করে তোলে। এটি thread-unsafe (থ্রেড সেফ নয়), তবে এটি দ্রুত এবং সহজ। যদি আপনাকে শুধুমাত্র একক থ্রেডে স্ট্রিং ম্যানিপুলেশন করতে হয়, তবে StringBuilder
সবচেয়ে ভালো পারফরম্যান্স প্রদান করে।
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World"); // StringBuilder modifies the same object
System.out.println(sb); // Output: Hello World
StringBuffer ক্লাসটি mutable (পরিবর্তনযোগ্য) এবং thread-safe (থ্রেড সেফ)। এটি StringBuilder এর মতোই কাজ করে, কিন্তু এর মধ্যে থ্রেড সেফটি রয়েছে। এর মানে হল যে এটি একাধিক থ্রেডের মধ্যে নিরাপদে ব্যবহার করা যায়, তবে কিছুটা পারফরম্যান্স খরচ হয়, কারণ এটি synchronization ব্যবহার করে।
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World"); // StringBuffer modifies the same object, with thread-safety
System.out.println(sb); // Output: Hello World
Criteria | String | StringBuilder | StringBuffer |
---|---|---|---|
Immutability | Immutable | Mutable | Mutable |
Thread-safety | Not thread-safe (except with StringPool ) | Not thread-safe | Thread-safe |
Performance | Slow for repeated concatenation | Faster for single-threaded use | Slower than StringBuilder due to synchronization |
Use case | When immutability is required (e.g., constants) | When string manipulation is frequent in single-threaded context | When string manipulation is needed in multi-threaded context |
Memory efficiency | Less efficient for frequent concatenation | More efficient than String in concatenation | Similar to StringBuilder , but with overhead of synchronization |
String
: When you need immutability, such as when storing constants or when the string will not be modified after creation (e.g., keys in a map).StringBuilder
: For most string manipulation tasks in a single-threaded environment, as it provides better performance and is more memory efficient than String
when concatenating strings.StringBuffer
: When you need thread-safety for string manipulation, such as when multiple threads may access the same string object concurrently.String
: Immutable, less efficient for repeated modifications.StringBuilder
: Mutable, best for single-threaded scenarios with high string manipulation.StringBuffer
: Mutable and thread-safe, but slower than StringBuilder
due to synchronization overhead.In general, StringBuilder
is recommended for most scenarios because of its performance advantages in single-threaded environments. However, if you are working in a multi-threaded context, you might prefer StringBuffer
due to its thread-safety. String
is ideal when you need immutability, such as for constant values.
Read more